#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<vector>
#include<queue>
using namespace std;
  struct ListNode {
      int val;
      ListNode *next;
      ListNode() : val(0), next(nullptr) {}
      ListNode(int x) : val(x), next(nullptr) {}
      ListNode(int x, ListNode *next) : val(x), next(next) {}
  };
 
class Solution {
    struct cmp
    {
        bool operator()(const ListNode* l1, const ListNode* l2)
        {
            return l1->val > l2->val;
        }
    };
public:
    ListNode* mergeKLists(vector<ListNode*>& lists) {
        priority_queue<ListNode*, vector<ListNode*>, cmp> as;
        for (auto l : lists)
        {
            if (l)
                as.push(l);
        }
        ListNode* qw = new ListNode(0);
        ListNode* qe = qw;
        while (!as.empty())
        {
            ListNode* w = as.top();
            as.pop();
            qe->next = w;
            qe = w;
            if (w->next)
            {
                as.push(w->next);;
            }
        }
        qe = qw->next;
        delete qw;
        return qe;

    }
};